package gitlab_hook

import (
	"encoding/json"
	"fmt"
	"testing"
)

var pipelineData = `
{
    "object_kind": "pipeline",
    "object_attributes": {
        "id": 86232,
        "ref": "master",
        "tag": false,
        "sha": "c2ac18263bd723883752d28d52427190851b2113",
        "before_sha": "baf6f136bb54dca2b2790b975fc5f39ff0471762",
        "source": "push",
        "status": "success",
        "detailed_status": "passed",
        "stages": [
            "build",
            "test",
            "deploy"
        ],
        "created_at": "2022-04-16 19:58:55 +0800",
        "finished_at": "2022-04-16 19:59:37 +0800",
        "duration": 40,
        "queued_duration": null,
        "variables": []
    },
    "merge_request": null,
    "user": {
        "id": 5094,
        "name": "pan leiming",
        "username": "panleiming",
        "avatar_url": null,
        "email": "[REDACTED]"
    },
    "project": {
        "id": 20535,
        "name": "Learn GitLab",
        "description": "Learn how to use GitLab to support your software development life cycle.",
        "web_url": "https://jihulab.com/lingxi/learn-gitlab",
        "avatar_url": "https://jihulab.com/uploads/-/system/project/avatar/20535/Artboard.jpg",
        "git_ssh_url": "git@jihulab.com:lingxi/learn-gitlab.git",
        "git_http_url": "https://jihulab.com/lingxi/learn-gitlab.git",
        "namespace": "lingxi",
        "visibility_level": 0,
        "path_with_namespace": "lingxi/learn-gitlab",
        "default_branch": "master",
        "ci_config_path": ""
    },
    "commit": {
        "id": "c2ac18263bd723883752d28d52427190851b2113",
        "message": "更新.gitlab-ci.yml文件",
        "title": "更新.gitlab-ci.yml文件",
        "timestamp": "2022-04-16T11:58:54+00:00",
        "url": "https://jihulab.com/lingxi/learn-gitlab/-/commit/c2ac18263bd723883752d28d52427190851b2113",
        "author": {
            "name": "pan leiming",
            "email": "skyknight928@163.com"
        }
    },
    "builds": [
        {
            "id": 2539793,
            "stage": "test",
            "name": "test2",
            "status": "success",
            "created_at": "2022-04-16 19:58:55 +0800",
            "started_at": "2022-04-16 19:59:14 +0800",
            "finished_at": "2022-04-16 19:59:24 +0800",
            "duration": 9.752847,
            "queued_duration": 0.634188,
            "when": "on_success",
            "manual": false,
            "allow_failure": false,
            "user": {
                "id": 5094,
                "name": "pan leiming",
                "username": "panleiming",
                "avatar_url": null,
                "email": "[REDACTED]"
            },
            "runner": {
                "id": 261,
                "description": "auto-scale-shared-runner-manager-5(0.82)",
                "runner_type": "instance_type",
                "active": true,
                "is_shared": true,
                "tags": [
                    "shared",
                    "docker",
                    "gitlab-org-docker"
                ]
            },
            "artifacts_file": {
                "filename": null,
                "size": null
            },
            "environment": null
        },
        {
            "id": 2539792,
            "stage": "test",
            "name": "test1",
            "status": "success",
            "created_at": "2022-04-16 19:58:55 +0800",
            "started_at": "2022-04-16 19:59:14 +0800",
            "finished_at": "2022-04-16 19:59:25 +0800",
            "duration": 11.353344,
            "queued_duration": 0.361698,
            "when": "on_success",
            "manual": false,
            "allow_failure": false,
            "user": {
                "id": 5094,
                "name": "pan leiming",
                "username": "panleiming",
                "avatar_url": null,
                "email": "[REDACTED]"
            },
            "runner": {
                "id": 258,
                "description": "auto-scale-shared-runner-manager-8(0.74)",
                "runner_type": "instance_type",
                "active": true,
                "is_shared": true,
                "tags": [
                    "shared",
                    "docker",
                    "gitlab-org"
                ]
            },
            "artifacts_file": {
                "filename": null,
                "size": null
            },
            "environment": null
        },
        {
            "id": 2539791,
            "stage": "build",
            "name": "build1",
            "status": "success",
            "created_at": "2022-04-16 19:58:55 +0800",
            "started_at": "2022-04-16 19:58:56 +0800",
            "finished_at": "2022-04-16 19:59:13 +0800",
            "duration": 17.86998,
            "queued_duration": 0.364123,
            "when": "on_success",
            "manual": false,
            "allow_failure": false,
            "user": {
                "id": 5094,
                "name": "pan leiming",
                "username": "panleiming",
                "avatar_url": null,
                "email": "[REDACTED]"
            },
            "runner": {
                "id": 264,
                "description": "auto-scale-shared-runner-manager-2(16.29)",
                "runner_type": "instance_type",
                "active": true,
                "is_shared": true,
                "tags": [
                    "shared",
                    "docker",
                    "gitlab-org-docker",
                    "release"
                ]
            },
            "artifacts_file": {
                "filename": null,
                "size": null
            },
            "environment": null
        },
        {
            "id": 2539794,
            "stage": "deploy",
            "name": "deploy1",
            "status": "success",
            "created_at": "2022-04-16 19:58:55 +0800",
            "started_at": "2022-04-16 19:59:26 +0800",
            "finished_at": "2022-04-16 19:59:37 +0800",
            "duration": 10.841131,
            "queued_duration": 0.463871,
            "when": "on_success",
            "manual": false,
            "allow_failure": false,
            "user": {
                "id": 5094,
                "name": "pan leiming",
                "username": "panleiming",
                "avatar_url": null,
                "email": "[REDACTED]"
            },
            "runner": {
                "id": 263,
                "description": "auto-scale-shared-runner-manager-3(16.4)",
                "runner_type": "instance_type",
                "active": true,
                "is_shared": true,
                "tags": [
                    "shared",
                    "docker",
                    "gitlab-org"
                ]
            },
            "artifacts_file": {
                "filename": null,
                "size": null
            },
            "environment": null
        }
    ]
}
`

func TestPipelineHook(t *testing.T) {
	hook := &PipelineHook{}
	err := json.Unmarshal([]byte(pipelineData), hook)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
	data, _ := json.MarshalIndent(hook, "  ", "  ")
	fmt.Println(string(data))
}
